﻿using System;
using System.Collections.Generic;
using System.Linq;

using MvcCms.Data.DynamicLINQ;
using MvcCms.Data.Pagination;
using MvcCms.Service.Cache;
using MvcCmsProjectTracker.Data;
using MvcCms.Data;
using MvcCms.Service.Logging;
using MvcCms.Service.Code;
using MvcCms.Service.Code.DataTables;
using Microsoft.Practices.ServiceLocation;
using MvcCms.Service;
using MvcCms.Service.Models;
using MvcCms.Service.Code.Email;

namespace MvcCmsProjectTracker.Service
{
    public class ProjectService : IProjectService
    {
        #region Constructors

        private readonly IProjectRepository _repository;
        private readonly ICacheService _cacheService;
        public ProjectService(IProjectRepository repository, ICacheService cacheService)
        {
            _repository = repository;
            _cacheService = cacheService;
        }

        #endregion

        public void Save()
        {
            try
            {
                _repository.Save();
            }
            catch (Exception ex)
            {
                AppLogging.GetLogger().Error(GeneralMethods.GetStackNames(), ex);
            }
        }

        #region List Methods
        public IQueryable<Project> GetAllProjects()
        {
            return _repository.GetAllProjects(_cacheService.GetCurrentPortal().PortalID);
        }
        public IQueryable<HelpPage> GetHelpPages()
        {
            return _repository.GetHelpPages(_cacheService.GetCurrentPortal().PortalID);
        }        
        public IQueryable<Project> GetMyProjects(Guid userId)
        {
            return _repository.GetMyProjects(userId);
        }
        public IQueryable<Project> GetHelpDeskProjects()
        {
            return _repository.GetHelpDeskProjects(_cacheService.GetCurrentPortal().PortalID);
        }
        public IQueryable<Project> GetNonHelpDeskProjects()
        {
            return _repository.GetNonHelpDeskProjects(_cacheService.GetCurrentPortal().PortalID);
        }        
        public IQueryable<ProjectCategory> GetProjectCategories(int projectID)
        {
            return _repository.GetProjectCategories(projectID);
        }
        public IQueryable<ProjectCustomField> GetProjectCustomFields(int projectID)
        {
            return _repository.GetProjectCustomFields(projectID);
        }
        public IQueryable<ProjectCustomFieldValue> GetProjectCustomFieldValues(int projectID)
        {
            return _repository.GetProjectCustomFieldValues(projectID);
        }
        public IQueryable<ProjectMember> GetProjectMembers(int projectID)
        {
            return _repository.GetProjectMembers(projectID);
        }
        public IQueryable<ProjectMilestone> GetProjectMilestones(int projectID)
        {
            return _repository.GetProjectMilestones(projectID);
        }
        public IQueryable<ProjectPriority> GetProjectPriorities(int projectID)
        {
            return _repository.GetProjectPriorities(projectID);
        }
        public IQueryable<ProjectStatus> GetProjectStatuses(int projectID)
        {
            return _repository.GetProjectStatuses(projectID);
        }
        #endregion

        #region Get Methods
        public Project GetProject(int projectID)
        {
            return _repository.GetProject(projectID);
        }
        public ProjectCategory GetProjectCategory(int categoryID)
        {
            return _repository.GetProjectCategory(categoryID);
        }
        public ProjectCustomField GetProjectCustomField(int customFieldID)
        {
            return _repository.GetProjectCustomField(customFieldID);
        }
        public ProjectCustomFieldValue GetProjectCustomFieldValue(int customFieldID)
        {
            return _repository.GetProjectCustomFieldValue(customFieldID);
        }
        public ProjectMember GetProjectMember(int projectID, Guid userId)
        {
            return _repository.GetProjectMember(projectID, userId);
        }
        public ProjectMember GetProjectMember(Guid userId)
        {
            return _repository.GetProjectMember(userId);
        }
        public ProjectMilestone GetProjectMilestone(int milestoneID)
        {
            return _repository.GetProjectMilestone(milestoneID);
        }
        public ProjectPriority GetProjectPriority(int priorityID)
        {
            return _repository.GetProjectPriority(priorityID);
        }
        public ProjectStatus GetProjectStatus(int statusID)
        {
            return _repository.GetProjectStatus(statusID);
        }
        #endregion

        #region Add Methods
        public bool AddProject(Project project)
        {
            project.PortalID = _cacheService.GetCurrentPortal().PortalID;
            try
            {
                _repository.AddProject(project);
            }
            catch (Exception ex)
            {
                AppLogging.GetLogger().Error(GeneralMethods.GetStackNames(), ex);
                return false;
            }
            return true;
        }
        public bool AddHelpPage(HelpPage helpPage)
        {
            helpPage.PortalID = _cacheService.GetCurrentPortal().PortalID;
            try
            {
                _repository.AddHelpPage(helpPage);
            }
            catch (Exception ex)
            {
                AppLogging.GetLogger().Error(GeneralMethods.GetStackNames(), ex);
                return false;
            }
            return true;
        }
        public bool AddProjectCategory(ProjectCategory category)
        {

            try
            {
                _repository.AddProjectCategory(category);
            }
            catch (Exception ex)
            {
                AppLogging.GetLogger().Error(GeneralMethods.GetStackNames(), ex);
                return false;
            }
            return true;
        }
        public bool AddProjectCustomField(ProjectCustomField customField)
        {

            try
            {
                _repository.AddProjectCustomField(customField);
            }
            catch (Exception ex)
            {
                AppLogging.GetLogger().Error(GeneralMethods.GetStackNames(), ex);
                return false;
            }
            return true;
        }
        public bool AddProjectCustomFieldValue(ProjectCustomFieldValue customFieldValue)
        {

            try
            {
                _repository.AddProjectCustomFieldValue(customFieldValue);
            }
            catch (Exception ex)
            {
                AppLogging.GetLogger().Error(GeneralMethods.GetStackNames(), ex);
                return false;
            }
            return true;
        }
        public bool AddProjectMember(ProjectMember projectMember)
        {

            try
            {
                _repository.AddProjectMember(projectMember);
            }
            catch (Exception ex)
            {
                AppLogging.GetLogger().Error(GeneralMethods.GetStackNames(), ex);
                return false;
            }
            return true;
        }
        public bool AddProjectMilestone(ProjectMilestone milestone)
        {

            try
            {
                _repository.AddProjectMilestone(milestone);
            }
            catch (Exception ex)
            {
                AppLogging.GetLogger().Error(GeneralMethods.GetStackNames(), ex);
                return false;
            }
            return true;
        }
        public bool AddProjectPriority(ProjectPriority priority)
        {
            try
            {
                _repository.AddProjectPriority(priority);
            }
            catch (Exception ex)
            {
                AppLogging.GetLogger().Error(GeneralMethods.GetStackNames(), ex);
                return false;
            }
            return true;
        }
        public bool AddProjectStatus(ProjectStatus status)
        {
            try
            {
                _repository.AddProjectStatus(status);
            }
            catch (Exception ex)
            {
                AppLogging.GetLogger().Error(GeneralMethods.GetStackNames(), ex);
                return false;
            }
            return true;
        }
        #endregion

        #region Delete Methods
        public bool DeleteProject(Project project)
        {
            try
            {
                _repository.DeleteProject(project);
            }
            catch (Exception ex)
            {
                AppLogging.GetLogger().Error(GeneralMethods.GetStackNames(), ex);
                return false;
            }
            return true;
        }
        public bool DeleteProjectCategory(ProjectCategory category)
        {
            try
            {
                _repository.DeleteProjectCategory(category);
            }
            catch (Exception ex)
            {
                AppLogging.GetLogger().Error(GeneralMethods.GetStackNames(), ex);
                return false;
            }
            return true;
        }
        public bool DeleteProjectCustomField(ProjectCustomField customField)
        {
            try
            {
                _repository.DeleteProjectCustomField(customField);
            }
            catch (Exception ex)
            {
                AppLogging.GetLogger().Error(GeneralMethods.GetStackNames(), ex);
                return false;
            }
            return true;
        }
        public bool DeleteProjectCustomFieldValue(ProjectCustomFieldValue customFieldValue)
        {
            try
            {
                _repository.DeleteProjectCustomFieldValue(customFieldValue);
            }
            catch (Exception ex)
            {
                AppLogging.GetLogger().Error(GeneralMethods.GetStackNames(), ex);
                return false;
            }
            return true;
        }
        public bool DeleteProjectMember(ProjectMember projectMember)
        {
            try
            {
                _repository.DeleteProjectMember(projectMember);
            }
            catch (Exception ex)
            {
                AppLogging.GetLogger().Error(GeneralMethods.GetStackNames(), ex);
                return false;
            }
            return true;
        }
        public bool DeleteProjectMilestone(ProjectMilestone milestone)
        {
            try
            {
                _repository.DeleteProjectMilestone(milestone);
            }
            catch (Exception ex)
            {
                AppLogging.GetLogger().Error(GeneralMethods.GetStackNames(), ex);
                return false;
            }
            return true;
        }
        public bool DeleteProjectPriority(ProjectPriority priority)
        {
            try
            {
                _repository.DeleteProjectPriority(priority);
            }
            catch (Exception ex)
            {
                AppLogging.GetLogger().Error(GeneralMethods.GetStackNames(), ex);
                return false;
            }
            return true;
        }
        public bool DeleteProjectStatus(ProjectStatus status)
        {
            try
            {
                _repository.DeleteProjectStatus(status);
            }
            catch (Exception ex)
            {
                AppLogging.GetLogger().Error(GeneralMethods.GetStackNames(), ex);
                return false;
            }
            return true;
        }
        #endregion

    }
}